package org.xshop.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import org.xshop.mapper.TSellerMapper;
import org.xshop.model.TSeller;
import org.xshop.model.TSellerBank;
import org.xshop.model.TSellerBrand;
import org.xshop.model.TSellerBusiness;
import org.xshop.model.TSellerLicense;
import org.xshop.model.TSellerOrganization;
import org.xshop.model.TSellerQualification;
import org.xshop.model.TSellerTaxCertificate;
import org.xshop.service.ITSellerService;

import com.weibo.api.motan.config.springsupport.annotation.MotanService;

import top.ibase4j.core.base.BaseServiceImpl;
import top.ibase4j.core.util.InstanceUtil;

/**
 * <p>
 * 商家  服务实现类
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-03-01
 */
@MotanService(interfaceClass = ITSellerService.class)
@CacheConfig(cacheNames = "TSeller")
public class SellerService extends BaseServiceImpl<TSeller, TSellerMapper> implements ITSellerService {
    @Autowired
    private SellerBankService bankService;
    @Autowired
    private SellerBrandService brandService;
    @Autowired
    private SellerBusinessService businessService;
    @Autowired
    private SellerLicenseService licenseService;
    @Autowired
    private SellerOrganizationService organizationService;
    @Autowired
    private SellerQualificationService qualificationService;
    @Autowired
    private SellerTaxCertificateService taxCertificateService;

    @Override
    @Transactional
    public TSeller update(TSeller record) {
        if (record.getEnable() == null) {
            record.setEnable(0);
        }
        TSeller company = super.update(record);
        Long id = company.getId();
        Map<String, Object> columnMap = InstanceUtil.newHashMap("seller_id", id);
        if (record.getBank() != null) {
            record.getBank().setSellerId(id);
            bankService.update(record.getBank());
        }
        brandService.deleteByMap(columnMap);
        if (record.getBrands() != null) {
            for (TSellerBrand brand : record.getBrands()) {
                brand.setId(null);
                brand.setSellerId(id);
                brandService.update(brand);
            }
        }
        businessService.deleteByMap(columnMap);
        if (record.getBusinesses() != null) {
            for (TSellerBusiness business : record.getBusinesses()) {
                business.setId(null);
                business.setSellerId(id);
                businessService.update(business);
            }
        }
        if (record.getLicense() != null) {
            record.getLicense().setSellerId(id);
            licenseService.update(record.getLicense());
        }
        if (record.getOrganization() != null) {
            record.getOrganization().setSellerId(id);
            organizationService.update(record.getOrganization());
        }
        qualificationService.deleteByMap(columnMap);
        if (record.getQualifications() != null) {
            for (TSellerQualification qualification : record.getQualifications()) {
                qualification.setId(null);
                qualification.setSellerId(id);
                qualificationService.update(qualification);
            }
        }
        if (record.getTaxCertificate() != null) {
            record.getTaxCertificate().setSellerId(id);
            taxCertificateService.update(record.getTaxCertificate());
        }
        return company;
    }

    @Override
    public TSeller queryById(Long id) {
        TSeller company = super.queryById(id);

        if (company != null) {
            TSellerBank bank = new TSellerBank();
            bank.setSellerId(company.getId());
            company.setBank(bankService.selectOne(bank));

            TSellerBrand brand = new TSellerBrand();
            brand.setSellerId(company.getId());
            company.setBrands(brandService.queryList(brand));

            TSellerBusiness business = new TSellerBusiness();
            business.setSellerId(company.getId());
            company.setBusinesses(businessService.queryList(business));

            TSellerLicense license = new TSellerLicense();
            license.setSellerId(company.getId());
            company.setLicense(licenseService.selectOne(license));

            TSellerOrganization organization = new TSellerOrganization();
            organization.setSellerId(company.getId());
            company.setOrganization(organizationService.selectOne(organization));

            TSellerQualification qualification = new TSellerQualification();
            qualification.setSellerId(company.getId());
            company.setQualifications(qualificationService.queryList(qualification));

            TSellerTaxCertificate taxCertificate = new TSellerTaxCertificate();
            taxCertificate.setSellerId(company.getId());
            company.setTaxCertificate(taxCertificateService.selectOne(taxCertificate));
        }

        return company;
    }

    @Transactional
    public TSeller updateSeller(TSeller record) {
        return super.update(record);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        Map<String, Object> columnMap = InstanceUtil.newHashMap("seller_id", id);
        bankService.deleteByMap(columnMap);
        brandService.deleteByMap(columnMap);
        businessService.deleteByMap(columnMap);
        licenseService.deleteByMap(columnMap);
        organizationService.deleteByMap(columnMap);
        qualificationService.deleteByMap(columnMap);
        taxCertificateService.deleteByMap(columnMap);
        super.delete(id);
    }
}
